/////////////
///////// CHAT
/////////
$(function() {
    tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins : "emotions,spellchecker,advhr,insertdatetime,preview", 
                
        // Theme options - button# indicated the row# only
        theme_advanced_buttons1 : "bold,italic,underline,fontselect,fontsizeselect,formatselect,forecolor,backcolor,link,unlink,|,spellchecker,advhr,removeformat,cleanup,code,|,sub,sup,|,charmap,emotions",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",      
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : false,
        width: "600",
        height: "100",
        skin : "o2k7",
        skin_variant : "silver"
    //force_br_newlines : true,
    //force_p_newlines : false
    });    
    
    $('#chkScroll').attr("checked", "checked");
    //
    $('#msgContainer').css("width", (screen.width - 300 + "px"));
    //$('#msgTable').css("width", "50%");
    $('#optionsContainer').css("width", (screen.width - 300 + "px"));
    
    //
    //$('#msgContainer').tinyscrollbar();
    //
    var offset = $("#usrContainer").offset();
    var topPadding = 15;
    $(window).scroll(function() {
        if ($(window).scrollTop() > offset.top) {
            $("#usrContainer").stop().animate({
                marginTop: $(window).scrollTop() - offset.top + topPadding
            });
        }
        else {
            $("#usrContainer").stop().animate({
                marginTop: 0
            });
        };
    
    });
});

var ajaxLock = false;

function sendMessage(){
    text = tinyMCE.getInstanceById('msg').getContent();
    //tinyMCE.getInstanceById('textareaid').setContent('');
    if(text.length >= 3 && text.length <= 1000){
        if (!ajaxLock) {
            ajaxLock = true;
            $('#msgSending').css("visibility", "visible");
            $.ajax({
                url: "core/ajax.jsp",
                data: 
                {
                    "action":"sendMsg",
                    "text":text
                },
                type: "POST",
                dataType: "json",
                async: "true",
                success: function(data){           
                    if (data.result == "success") {
                        chatRefresh();
                    }
                    
                    if(data.result == "session_expired"){ 
                        showModalWindowRedirect("Сообщение", "Ваша сессия не найдена", "auth.jsp", 500, 200);
                    }
                }
            });
            tinyMCE.getInstanceById('msg').setContent('');
            $('#msgSending').css("visibility", "hidden");
            ajaxLock = false;    
        } else {
            alert("ajaxLock");
        }

    }else{
        showModalWindow("Сообщение", "Минимальная длинна сообщения 3 символа", 500, 200);
    }
}

function chatRefresh(){
    if (!ajaxLock) {
        ajaxLock = true;
        $.ajax({
            url: "core/ajax.jsp",
            data: 
            {
                "action":"getChat"
            },
            type: "POST",
            dataType: "json",
            async: "true",
            success: function(data){           
                if (data.result == "success") {
                    $('#msgContainer').html(data.html_chat);
                    $('#userList').html(data.html_users);
                    doScroll();
                //var obj = jQuery.parseJSON(data.msgArray);
                }
                
                if(data.result == "session_expired"){ 
                    showModalWindowRedirect("Сообщение", "Ваша сессия не найдена", "auth.jsp", 500, 200);
                }
            }
        });         
        ajaxLock = false;
    } else {
        alert("ajaxLock");
    }
}

var isScroll = true;
function enableScroll(){
    isScroll = true
}
function disableScroll(){
    isScroll = false;
}
function doScroll(){
    if(isScroll == true){
        $('#msgContainer').scrollTo( '100%', 800 );
    }
}

function changeMsgScroll(){
    if ($('#chkScroll').attr("checked") == "checked") {
        enableScroll();
    } else {
        disableScroll();
    }
}

function usrContainerMinimize(){
   
}

function usrContentMin(){
    $('#usrContainer').animate({
        opacity: 0.25,
        height: '50px'
    }, 1000, function() {
        // Animation complete.
        }); 
}

function usrContentMax(){
    $('#usrContainer').animate({
        opacity: 1,
        height: '435px'
    }, 1000, function() {
        // Animation complete.
        });   
}